#include "Fl_Gl_ITK.h"


namespace FPTK{

void Fl_Gl_ITK::Init()
{
    W=0;
    H=0;
    D=0;
    imagei=NULL;
    calc=0;
    pos=NULL;
    loaded=0;
    ptreader=NULL;
    nbslice=0;
    max=0;
    setdepth=0;
    setcontrast=0;
}



Fl_Gl_ITK::Fl_Gl_ITK(int W, int H):Fl_Gl_Window(W,H)
{
Init();
}




Fl_Gl_ITK::Fl_Gl_ITK(int X, int Y, int W, int H, const char *l):Fl_Gl_Window(X,Y,W,H,l)
{
Init();
}

Fl_Gl_ITK::~Fl_Gl_ITK()
{
delete imagei;
delete pos;
}




void Fl_Gl_ITK::draw()
{
    if(setcontrast || setdepth)
    {
        for(int i=0;i<H*W;i++)
        {
            if(*(nbslice*W*H+imagei+i)*max/contrastvalue<256)
            {
                *(pos+i)=*(nbslice*W*H+imagei+i)*max/contrastvalue;
            }
            else
            {
                *(pos+i)=255;
            }
        }
        setdepth=0;
        setcontrast=0;
    }
    int window_W=w();
    int window_H=h();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
	glPushMatrix();

    glClear(GL_COLOR_BUFFER_BIT); 
    glOrtho(0,window_W,0,window_H,0,1);
    glColor4f(0.5,0.5,0,.5);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
    glEnable(GL_BLEND);
    glRecti(0,0,calc*window_W/W,window_H);
    float x=float(window_W)/float(W);
    float y=float(window_H)/float(H);
    if(x>y)
       {
       x=y;
       }
    else
       {
       y=x;
       }
    glPixelZoom(x,y);
    glRasterPos2i(0,0);
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    glDrawPixels(W,H,GL_LUMINANCE, GL_UNSIGNED_BYTE, pos);

    /**/
    
    
    //glDrawPixels(W,H,GL_RGBA, GL_UNSIGNED_BYTE , calc);
    glPopMatrix();
}





int Fl_Gl_ITK::LoadImage(char *filename)
{
    if(fopen(filename,"r"))
    {
        ptreader = ReaderTypeImageFL_GL_ITK::New();
        ptreader->SetFileName( filename );
        ptreader->Update();
        sizei=ptreader->GetOutput()->GetLargestPossibleRegion().GetSize();
        loaded=1;
    }
    else
    {
        return -1;
    }
IteratorType it(ptreader->GetOutput(),ptreader->GetOutput()->GetLargestPossibleRegion());
max=0;
for(it.GoToBegin();!it.IsAtEnd();++it)
{
    if(it.Get()>max)
    {
        max=it.Get();
    }
}
contrastvalue=max;
W=sizei[1];
H=sizei[0];
D=sizei[2];
if(imagei)
   {
   free(imagei);
   imagei=NULL;
   }
//imagei =(GLubyte *) calloc(H*W,sizeof(char));
imagei=(GLubyte*)new char[H*W*D];
pos=(GLubyte *)new char[W*H];
   if(!imagei)
   {
       loaded=0;
       return -1;
   }
    if(loaded)
    {
        SliceIteratorType it2(ptreader->GetOutput(),ptreader->GetOutput()->GetLargestPossibleRegion());
        it2.SetFirstDirection(1);
        it2.SetSecondDirection(0);
        int i;
        for(i=0,it2.GoToBegin();!it2.IsAtEnd();it2.NextSlice())
        {
            for(;!it2.IsAtEndOfSlice();it2.NextLine())
            {
            for(;!it2.IsAtEndOfLine();++it2,i++)
                {
                *(imagei+i)=char(double(it2.Get()*256)/max);
                }
            }
        }
    }
return 0;
}





int Fl_Gl_ITK::handle(int event)
{       if(Fl::event_button2())
          {
Fl::error("\n\n       Filter over    \n\n");
          }
    return 0;
}

void Fl_Gl_ITK::SetSlice(int nb)
{
    nbslice=nb;
    setdepth=1;
}

int Fl_Gl_ITK::GetDepth()
{
    return D;
}

int Fl_Gl_ITK::GetHeight()
{
    return H;
}
int Fl_Gl_ITK::GetWidth()
{
    return W;
}

int Fl_Gl_ITK::GetConstrastMax()
{
    return max;
}

void Fl_Gl_ITK::SetContrast(int value)
{
    setcontrast=1;
    if(value<max && value>0)
    {
        contrastvalue=value;
    }
    else if(value>=max)
    {
        contrastvalue=max;
    }
    else
    {
        contrastvalue=0;
    }
}

void Fl_Gl_ITK::SetCalc(int value)
{
    if(value<W && value>=0)
    {
        calc=value;
    }
}

} //end namespace FPTK